from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..config.database import Base
from .user import user_chat_room


class ChatRoom(Base):
    __tablename__ = "chat_rooms"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False)
    description = Column(String, nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 与用户的多对多关系
    members = relationship("User", secondary=user_chat_room, back_populates="chat_rooms")
    
    # 与聊天消息的一对多关系
    messages = relationship("ChatMessage", back_populates="chat_room", cascade="all, delete-orphan")